package cn.org.book.shop.app.controller;

import cn.org.book.shop.article.domain.ApArticle;
import cn.org.book.shop.article.domain.ApArticleDataConfiguration;
import cn.org.book.shop.article.domain.ApDownload;
import cn.org.book.shop.article.domain.ApMaterial;
import cn.org.book.shop.article.service.IApArticleDataConfigurationService;
import cn.org.book.shop.article.service.IApArticleService;
import cn.org.book.shop.article.service.IApDownloadService;
import cn.org.book.shop.article.service.IApMaterialService;
import cn.org.book.shop.common.core.domain.AjaxResult;
import cn.org.book.shop.common.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/v1/app/article")
public class AppArticleController extends AppBaseController {
    /**
     * 数据查询接口
     */
    @Resource
    private IApArticleDataConfigurationService apArticleDataConfigurationService;

    @Resource
    private IApMaterialService apMaterialService;

    @Resource
    private IApArticleService apArticleService;
    @Resource
    private IApDownloadService apDownloadService;

    /**
     * 根据关键字查询绘本列表
     *
     * @return 绘本列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ApArticleDataConfiguration apArticle) {
        startPage();
        List<ApArticleDataConfiguration> list = apArticleDataConfigurationService.selectApArticleDataConfigurationList(apArticle);
        List list1 = changeApArticleDataConfiguration(list);
        return getDataTable(list1, list);
    }




    @GetMapping("{articleId}")
    public AjaxResult getApArticleContent(@PathVariable Long articleId) {
        ApArticle apArticle = apArticleService.selectApArticleById(articleId);
        ApDownload apDownload = new ApDownload();
        apDownload.setArticleId(articleId);
        List<ApDownload> apDownloads = apDownloadService.selectApDownloadList(apDownload);
        ApMaterial material = new ApMaterial();
        material.setArticleId(articleId);
        List<ApMaterial> apMaterials = apMaterialService.selectApMaterialList(material);
        apArticle.setApMaterials(apMaterials);
        apArticle.setApDownload(!apDownloads.isEmpty() ? apDownloads.get(0) : null);
        return AjaxResult.success(apArticle);
    }


}
